import unittest
from citeparser import CiteParser, Authors

# test1 : Hodgkin A.L., Huxley A.F., and Katz B. (1952) Measurement of current-voltage relations in the membrane of the giant axon of Loligo. J. Physiol. 115(4):424-48
class CiteParserTestCase(unittest.TestCase):
    def setUp(self):
        self.cparser = CiteParser()
        self.cparser.throwErrors = True

    def tearDown(self):
        self.cparser = None
    def test1(self):
        authors = 'Hodgkin A.L., Huxley A.F., and Katz B.'
        self.__parseAuthors(authors)

        year = 1952
        title = 'Measurement of current-voltage relations in the membrane of the giant axon of Loligo.'
        journal = 'J. Physiol.'
        volume = 115
        issue = 4
        firstpage = 424
        lastpage = 48
        text = '%s (%d) %s %s %d(%d):%d-%d' % (
            authors,year,title,journal,volume,issue,firstpage,lastpage)
        self.__parseText( text, 
            {'Authors':authors,
             'Year':str(year),
             'Title':title,
             'Journal':journal,
             'Volume':str(volume),
             'Issue':str(issue),
             'FirstPage':str(firstpage),
             'LastPage':str(lastpage) })
    def test2(self):
        self.__parseAuthors('Gottlieb, J.')
        self.__parseText( 
            'Gottlieb, J. From Thought to Action: The Parietal Cortex as a Bridge'+
            ' between Perception, Action, and Cognition.  Neuron 53(1): 9-16 (2007)',
            {'Title': 'From Thought to Action: The Parietal Cortex as a '+
                'Bridge between Perception, Action, and Cognition.'}) 
    def test3(self):
        self.__parseText(
            'Cao, J. and Nunamaker, J. F. (2004). Question answering on lecture '+
            'videos: a multifaceted approach. Proc. ACM/IEEE Joint Conference on '+
            'Digital Libraries.',
            {'Title':'Question answering on lecture videos: a multifaceted approach.'})
    def testCrises1(self):
        self.__parseAuthors('Chossat P., and Golubitsky M.')
        self.__parseText(
            'Chossat P., and Golubitsky M. (1988) '+
            'Symmetry Increasing Bifurcation of Chaotic Attractors. Physica D 32, 423.',
            {'Volume':'32','Journal':'Physica D'})
    def testCrises2(self):
        self.__parseAuthors('Ditto W. L. et al.')
        self.__parseText(
            'Ditto W. L. et al. (1989) Experimental Observation of Crisis-Induced '+
            'Intermittency and its Critical Exponent. Phys. Rev. Lett. 63, 923.',
            {'Volume':'63','Journal':'Phys. Rev. Lett.'})
    def __parseText(self,text,checks):
        parsed = self.cparser.parse(text)
        for var,val in checks.iteritems():
            assert parsed[var]==val, (
                '%s not parsed correctly.\nShould be: %s\nParsed as: %s'%
                (var,val,parsed[var]))
    def __parseAuthors(self,authors):
        try:
            parsed = Authors.parseString(authors)
            if parsed['Authors'] != authors:
                raise Exception('Authors should be "%s" but parsed as "%s"'%(
                    authors, parsed['Authors']))
        except Exception:
            raise Exception('Failed to parse the authors "%s"'%(authors))

def suite():
    suite = unittest.TestSuite()
    suite.addTest(CiteParserTestCase("test1"))
    suite.addTest(CiteParserTestCase("test2"))
    suite.addTest(CiteParserTestCase("test3"))
    suite.addTest(CiteParserTestCase("testCrises1"))
    suite.addTest(CiteParserTestCase("testCrises2"))
    return suite

def run_suite():
    runner = unittest.TextTestRunner()
    runner.run( suite() )
